package com.example.administrator.nurse.NetWork;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

public class RequestManager {

    private static AsyncHttpClient client = new AsyncHttpClient();

    static {
        client.setTimeout(30000);// 设置链接超时，如果不设置，默认为10s
    }

    public static AsyncHttpClient getClient() {
        return client;
    }


    /**
     * Get请求
     */
    public static void get(final int requestCode, final Object object, String url, RequestParams params,
                           final RequestCallback callback) {
        client.get(url, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString,Throwable throwable) {
                callback.onFailureCallback(requestCode,object, responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                callback.onSuccessCallback(requestCode,object, responseString);
            }
        });
    }


    public static void post
            (
                final int requestCode,
                final Object object,
                String url,
                RequestParams params,
                final RequestCallback callback
            )
    {


        client.post(url, params, new TextHttpResponseHandler() {
//            params.put("appMark", CommonDataUtils.getAppMark());

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString,Throwable throwable) {
                callback.onFailureCallback(requestCode,object, responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("zzzzz", responseString);
                callback.onSuccessCallback(requestCode,object, responseString);
            }
        });
    }

//    public static void delete(final int requestCode, String url, RequestParams params,
//                              final RequestCallback callback) {
//        client.delete(url, params, new TextHttpResponseHandler() {
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                callback.onFailureCallback(requestCode, responseString);
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                callback.onSuccessCallback(requestCode, responseString);
//            }
//        });
//    }


//    /**
//     * 带参数请求
//     */
//    public static void post(String url, HttpEntity entity, String mimeType,
//                            AsyncHttpResponseHandler handler) {
//        client.post
//                (
//                        common.context,
//                        common.host + url,
//                        entity,
//                        mimeType,
//                        handler
//                );
//
//    }

}
